package com.xiaoma.kent.webservice;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaoma.kent.dto.UserDto;
import com.xiaoma.kent.entity.User;
import com.xiaoma.kent.service.UserService;

@Controller
@RequestMapping("/user")
public class DemoWS {

    @Autowired
    private UserService userService;
    
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<User> findUsers(UserDto userDto) {
        User entity = new User();
        entity.setAge(userDto.getAge());
        entity.setName(userDto.getName());
        entity.setGender(userDto.getGender());
        return userService.findUsers(entity);
    }
    
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveUser(@RequestBody UserDto userDto) {
        User user = new User();
        user.setName(userDto.getName());
        user.setAge(userDto.getAge());
        user.setGender(userDto.getGender());
        user.setCreatedAt(new Date());
        userService.save(user);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("status", 1);
        return result;
    }
}
